package com.baiyun.bs.service.impl;

import com.baiyun.bs.domain.Notice;
import com.baiyun.bs.exception.BusinessException;
import com.baiyun.bs.repository.NoticeMapper;
import com.baiyun.bs.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

import java.util.Date;

/**
 * @author by wzz
 * @implNote 2020/10/20 9:05
 */
@Service
public class NoticeServiceImpl implements NoticeService {
    private final Object lock = new Object();

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public String getCurrentNotice() {
        Notice notice = null;
        if (notice != null) {
            return notice.getContent();
        }else {
            return null;
        }
    }

    @Override
    public List<Notice> getAllNotices(Notice notice) {
        return noticeMapper.selectOne(notice);
    }



    @Override
    public void deleteNotice(Long nId) {
        // 转换成数组 需要操作的用户的id数组
        noticeMapper.deleteNotice(nId);
    }

    @Override
    public void updateNotice(Notice notice) {
        noticeMapper.updateNotice(notice);

    }

    @Override
    public void addNotice(Notice notice) {
        // 检查是否已存在相同数据
        synchronized (lock) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String createTime = sdf.format(new Date());
            notice.setCreateTime(createTime);

            // 设置其他属性等逻辑
            // ...

            noticeMapper.insertNotice(notice);
        }
    }

    @Override
    public Notice selectNoticeById(Long nId) {
        return noticeMapper.selectNoticeById(nId);
    }
}
